<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是C语言——指针相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-06-C语言-指针">
<meta property="og:url" content="https://sumumm.github.io/post/84847102.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是C语言——指针相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130130321406.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130141024744.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130174049078.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130180725096.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130181429628.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130182415239.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130210001974.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204134546751.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204155825156.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204190231704.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204195558425.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204203431783.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204204857551.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204210115901.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206154923623.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206165514337.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206172020146.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220207104831017.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220207105826214.png">
<meta property="article:published_time" content="2022-01-15T08:29:55.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:56.984Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV01-C语言基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130130321406.png">


<link rel="canonical" href="https://sumumm.github.io/post/84847102.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/84847102.html","path":"post/84847102.html","title":"LV01-06-C语言-指针"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-06-C语言-指针 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%8C%87%E9%92%88"><span class="nav-text">一、指针</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E6%8C%87%E9%92%88%EF%BC%9F"><span class="nav-text">1.什么是指针？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%B8%80%E5%88%87%E7%9A%86%E5%9C%B0%E5%9D%80%EF%BC%9F"><span class="nav-text">2.一切皆地址？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E7%94%A8%E6%8C%87%E9%92%88%EF%BC%9F"><span class="nav-text">3.为什么要用指针？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8%E6%8C%87%E9%92%88%EF%BC%9F"><span class="nav-text">4.怎么使用指针？</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-text">4.1指针变量的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E8%B5%8B%E5%80%BC"><span class="nav-text">4.2指针变量的赋值</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E5%BC%95%E7%94%A8"><span class="nav-text">4.3指针变量的引用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-4%E6%8C%87%E9%92%88%E5%92%8C%E5%8F%98%E9%87%8F%E5%9C%A8%E5%86%85%E5%AD%98%E4%B8%AD%E7%9A%84%E5%AD%98%E6%94%BE"><span class="nav-text">4.4指针和变量在内存中的存放</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E6%8C%87%E9%92%88%E5%8D%A0%E5%87%A0%E4%B8%AA%E5%AD%97%E8%8A%82%EF%BC%9F"><span class="nav-text">5.指针占几个字节？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%90%E7%AE%97"><span class="nav-text">6.指针的运算</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1%E6%8C%87%E9%92%88%E7%9A%84%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97"><span class="nav-text">6.1指针的赋值运算</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2%E6%8C%87%E9%92%88%E7%9A%84%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97"><span class="nav-text">6.2指针的算术运算</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-1%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97"><span class="nav-text">6.2.1算术运算</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">6.2.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-3%E6%8C%87%E9%92%88%E7%9A%84%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97"><span class="nav-text">6.3指针的关系运算</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-void-%E6%8C%87%E9%92%88"><span class="nav-text">7. void 指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#7-1-void-%E6%8C%87%E9%92%88%E6%A6%82%E5%BF%B5"><span class="nav-text">7.1 void 指针概念</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-2%E4%B8%80%E8%88%AC%E5%BD%A2%E5%BC%8F"><span class="nav-text">7.2一般形式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7-3%E4%BD%BF%E7%94%A8%E8%A7%84%E5%88%99"><span class="nav-text">7.3使用规则</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-const-%E6%8C%87%E9%92%88"><span class="nav-text">8. const 指针</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#8-1-const-%E5%8F%98%E9%87%8F"><span class="nav-text">8.1 const 变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-2%E5%B8%B8%E9%87%8F%E5%8C%96%E6%8C%87%E9%92%88%E7%9B%AE%E6%A0%87%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">8.2常量化指针目标表达式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-3%E5%B8%B8%E9%87%8F%E5%8C%96%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F"><span class="nav-text">8.3常量化指针变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#8-4%E5%B8%B8%E9%87%8F%E5%8C%96%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E5%8F%8A%E5%85%B6%E7%9B%AE%E6%A0%87%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-text">8.4常量化指针变量及其目标表达式  </span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%8C%87%E9%92%88%E4%B8%8E%E6%95%B0%E7%BB%84"><span class="nav-text">二、指针与数组</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%8C%87%E9%92%88%E4%B8%8E%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">1.指针与一维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1%E6%95%B0%E7%BB%84%E7%9A%84%E6%8C%87%E9%92%88"><span class="nav-text">1.1数组的指针</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E7%9A%84%E8%A1%A8%E7%A4%BA"><span class="nav-text">1.2数组元素的表示</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%8C%87%E9%92%88%E4%B8%8E%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">2.指针与二维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1%E5%88%97%E6%8C%87%E9%92%88%E9%81%8D%E5%8E%86%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">2.1列指针遍历二维数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2%E8%A1%8C%E6%8C%87%E9%92%88%E9%81%8D%E5%8E%86%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">2.2行指针遍历二维数组</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3%E8%A1%8C%E6%8C%87%E9%92%88%E4%B8%8E%E5%88%97%E6%8C%87%E9%92%88"><span class="nav-text">2.3行指针与列指针</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="nav-text">3.指针数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-text">3.1指针数组的定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">3.2指针数组的初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E7%9A%84%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4"><span class="nav-text">3.3指针数组的存储空间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E7%9A%84%E6%95%B0%E7%BB%84%E5%90%8D"><span class="nav-text">3.4指针数组的数组名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-5%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84%E4%B8%8E%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">3.5指针数组与二维数组</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E6%8C%87%E9%92%88%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-text">三、指针与字符串</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">1.字符指针变量的概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%AD%97%E7%AC%A6%E6%8C%87%E9%92%88%E5%8F%98%E9%87%8F%E7%9A%84%E8%B5%8B%E5%80%BC"><span class="nav-text">2.字符指针变量的赋值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%89%93%E5%8D%B0"><span class="nav-text">3.字符串打印</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E5%A4%9A%E7%BA%A7%E6%8C%87%E9%92%88"><span class="nav-text">四、多级指针</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%A4%9A%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">1.多级指针的概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BA%8C-%E5%A4%9A-%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E5%A3%B0%E6%98%8E"><span class="nav-text">2.二(多)级指针的声明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BA%8C-%E5%A4%9A-%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">3.二(多)级指针的初始化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E4%BA%8C-%E5%A4%9A-%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E5%BC%95%E7%94%A8"><span class="nav-text">4.二(多)级指针的引用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%A4%9A%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4"><span class="nav-text">5.多级指针的存储空间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%A4%9A%E7%BA%A7%E6%8C%87%E9%92%88%E7%9A%84%E8%BF%90%E7%AE%97"><span class="nav-text">6.多级指针的运算</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88"><span class="nav-text">五、函数指针</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="nav-text">1.函数指针的定义</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%9A%84%E5%A3%B0%E6%98%8E"><span class="nav-text">2.函数指针的声明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="nav-text">3.函数指针的使用</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">3.1函数指针初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%9A%84%E8%B0%83%E7%94%A8"><span class="nav-text">3.2函数指针的调用</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%9A%84%E5%AD%98%E5%82%A8%E7%A9%BA%E9%97%B4"><span class="nav-text">4.函数指针的存储空间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E7%B1%BB%E5%9E%8B"><span class="nav-text">5.函数指针类型</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1%E5%AE%9A%E4%B9%89%E6%A0%BC%E5%BC%8F"><span class="nav-text">5.1定义格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E5%87%BD%E6%95%B0%E6%8C%87%E9%92%88%E6%95%B0%E7%BB%84"><span class="nav-text">6.函数指针数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#6-1%E5%AE%9A%E4%B9%89"><span class="nav-text">6.1定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-2%E4%BD%BF%E7%94%A8"><span class="nav-text">6.2使用</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E6%8C%87%E9%92%88%E5%AE%9A%E4%B9%89%E6%80%BB%E7%BB%93"><span class="nav-text">六、指针定义总结</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-int-p"><span class="nav-text">1. int p; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-int-p"><span class="nav-text">2. int *p; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-int-p"><span class="nav-text">3. int **p; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-int-p-3"><span class="nav-text">4. int p[3]; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-int-p-3"><span class="nav-text">5. int *p[3]; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-int-p-3"><span class="nav-text">6. int (*p)[3]; </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-int-p-int"><span class="nav-text">7. int p(int); </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-int-p-int"><span class="nav-text">8. int *p(int); </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-int-p-int"><span class="nav-text">9. int (*p)(int); </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#10-int-p-3-int-int"><span class="nav-text">10. int (*p[3])(int, int); </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#11-int-p-int-3"><span class="nav-text">11. int *(*p(int))[3]; </span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/84847102.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-06-C语言-指针 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-06-C语言-指针
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-01-15 16:29:55" itemprop="dateCreated datePublished" datetime="2022-01-15T16:29:55+08:00">2022-01-15</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/" itemprop="url" rel="index"><span itemprop="name">01HQ课程体系</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" itemprop="url" rel="index"><span itemprop="name">LV01-C语言基础</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>15k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>53 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是C语言——指针相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">SecureCRT</td>        <td align="left">Version 8.7.2 (x64 build 2214)   -   正式版-2020年5月14日</td>      </tr>    <tr>        <td align="center">开发板</td>        <td align="left">正点原子 i.MX6ULL Linux阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">STM32开发板</td>        <td align="left">正点原子战舰V3(STM32F103ZET6)</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">---</td><td align="left"><a href="" target="_blank">--- <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<h1 id="一、指针"><a href="#一、指针" class="headerlink" title="一、指针"></a><font size=3>一、指针</font></h1><h2 id="1-什么是指针？"><a href="#1-什么是指针？" class="headerlink" title="1.什么是指针？"></a><font size=3>1.什么是指针？</font></h2><p>计算机中所有的数据都必须放在内存中，不同类型的数据占用的字节数不一样，例如  int  占用  4  个字节， char  占用 1 个字节。为了正确地访问这些数据，必须为每个字节都编上号码，就像门牌号、身份证号一样，每个字节的<strong>编号</strong>是<strong>唯一</strong>的，根据编号可以准确地找到某个字节。</p>
<p>内存中字节的编号称为<strong>地址（ Address ）</strong>或<strong>指针（ Pointer ）</strong>。在C语言中，内存单元的地址称为指针，专门用来<strong>存放地址的变量</strong>，称为<strong>指针变量</strong>，在不影响理解的情况中，有时对地址、指针和指针变量不区分，通称指针。<br>$$<br>地址 &#x3D; 指针 &#x3D; 指针变量<br>$$<br>对于  32 位 环境，程序能够使用的内存为  4GB ，最小的地址为  0x00000000 ，最大的地址为  0xFFFFFFFF ，注意这里的地址都是用 8 个十六进制数表示的，一共是 32 位。</p>
<p>对于 64 位系统来说，我们打印地址的时候会发现，它的地址都是由 12 个十六进制数表示的，这样算下来，才 48 位，按理来说不应该是 64 位吗？通过查阅资料，发现 48 位其实只是表象，显示了 48 位是因为目前为止 64 位系统的地址线只有 48 条。地址中第 48 位到第 63 位由第 47 位扩展而来（<strong>全0 全1</strong>）。因此有两段合法的地址空间，分别是</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0x0000 0000 0000 0000 - 0x0000 7FFF FFFF FFFF</span><br><span class="line">0xFFFF 8000 0000 0000 - 0xFFFF FFFF FFFF FFFF</span><br></pre></td></tr></table></figure>

<p>两段加起来总共 2^48Byte &#x3D; 256TB ，一段是 128TB ，然而现在的 PC 基本达不到 128  的内存，因此第二段地址一般是见不到的，全都存在了第一段当中，所以看到的 48 位地址应该在前边再加上 0000 这才是完整的 64 位地址。</p>
<h2 id="2-一切皆地址？"><a href="#2-一切皆地址？" class="headerlink" title="2.一切皆地址？"></a><font size=3>2.一切皆地址？</font></h2><p> C语言 用变量来存储数据，用函数来定义一段可以重复使用的代码，它们最终都要放到内存中才能供  CPU  使用。 CPU  只能通过<strong>地址</strong>来取得内存中的<strong>代码和数据</strong>，程序在执行过程中会告知  CPU  要执行的代码以及要读写的数据的地址。</p>
<p> CPU  访问内存时需要的是<strong>地址</strong>，而不是变量名和函数名。<strong>变量名和函数名只是地址的一种助记符</strong>，当源文件被编译和链接成可执行程序后，它们都会被替换成地址。编译和链接过程的一项重要任务就是找到这些名称所对应的地址。</p>
<p>例如：</p>
<p>变量  a、b、c  在内存中的地址分别是  0X0000、0X0004、0X0008 ，那么加法运算 c &#x3D; a + b; 将会被转换成类似下面的形式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">0X0008</span> = (<span class="number">0X0000</span>) + (<span class="number">0X0004</span>);</span><br></pre></td></tr></table></figure>

<p> ( ) 表示取值操作，整个表达式的意思是，取出地址  0X0000  和  0X0004  上的值，将它们相加，把相加的结果赋值给地址为  0X0008  的内存。</p>
<p>所以，从根本上来说，数据的运算其实都是通过地址来运算的。</p>
<h2 id="3-为什么要用指针？"><a href="#3-为什么要用指针？" class="headerlink" title="3.为什么要用指针？"></a><font size=3>3.为什么要用指针？</font></h2><p>C程序设计中使用指针可以</p>
<ul>
<li><p>使程序简洁、紧凑、高效。</p>
</li>
<li><p>有效地表示复杂的数据结构。</p>
</li>
<li><p>动态分配内存。</p>
</li>
<li><p>得到多于一个的函数返回值。</p>
</li>
</ul>
<h2 id="4-怎么使用指针？"><a href="#4-怎么使用指针？" class="headerlink" title="4.怎么使用指针？"></a><font size=3>4.怎么使用指针？</font></h2><h3 id="4-1指针变量的定义"><a href="#4-1指针变量的定义" class="headerlink" title="4.1指针变量的定义"></a><font size=3>4.1指针变量的定义</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">storage_type data_type *p_name;</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=100px>storage_type</td>
        <td align="left">存储类型（指针变量本身的存储类型,可以说明也可以不说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>data_type</td>
        <td align="left">任意有效的 C 数据类型（指针目标的数据类型，必须说明）</td>
    </tr>
    <tr>
        <td align="center" width=100px>p_name</td>
        <td align="left">指针变量名</td>
    </tr>
</table>


<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="type">int</span> *p1;  <span class="comment">/* p1是一个指向静态整型变量的指针变量 */</span></span><br><span class="line"><span class="type">float</span> *p2;       <span class="comment">/* p2是一个指向浮点型变量的指针变量 */</span></span><br><span class="line"><span class="type">char</span> *p3;        <span class="comment">/* p3是一个指向字符型变量的指针变量 */</span></span><br><span class="line"><span class="type">int</span> *a, *b, *c;  <span class="comment">/* a、b、c 的都是 int* 类型的指针变量 */</span></span><br><span class="line"><span class="type">int</span> *a, b, c;    <span class="comment">/* a 是 int* 类型的指针变量，b、c 都是类型为 int 的普通变量 */</span></span><br></pre></td></tr></table></figure>

<p>【注意】指针说明时指定的数据类型不是指针变量本身的数据类型，而是指针目标的数据类型，简称为指针的数据类型。</p>
<p>【说明】引入指针要注意程序中的 p 、 *p  和  &amp;p  三种表示方法的不同意义。设 p 为一个指针( int *p; )则：</p>
<table>
   <tr>
       <td align="center">p</td>
       <td align="left"> 指针变量， 它的内容是地址量</td>
   </tr>
    <tr>
       <td align="center">*p</td>
       <td align="left"> 指针所指向的对象， 它的内容是数据</td>
   </tr>
    <tr>
       <td align="center">&p</td>
       <td align="left"> 指针变量占用的存储区域的地址，是个常量</td>
   </tr>
</table>


<h3 id="4-2指针变量的赋值"><a href="#4-2指针变量的赋值" class="headerlink" title="4.2指针变量的赋值"></a><font size=3>4.2指针变量的赋值</font></h3><p>指针变量在使用前，不仅要<strong>定义说明</strong>，而且要<strong>赋予具体的值</strong>，如果没有确切的地址可以赋值，为指针变量赋一个  NULL  值（赋为  NULL  值的指针被称为<strong>空指针</strong>），未经赋值的指针不能随便使用，否则将导致程序的错误，并且指针变量的值只能是<strong>变量的地址</strong>，不能是其他数据，否则将会导致错误产生。</p>
<p>没有合法指向的指针称为<strong>“野”指针</strong>。“野”指针随机指向一块空间，该空间中存储的可能是其他程序的数据甚至是系统数据，故不能对“野”指针所指向的空间进行存取操作，否则轻者会引起程序崩溃，严重的可能导致整个系统崩溃。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 1. 定义时直接赋值 */</span></span><br><span class="line">&lt;存储类型&gt;  &lt;数据类型&gt;  *&lt;指针变量名&gt; = &lt;地址量&gt;; </span><br><span class="line"></span><br><span class="line"><span class="comment">/* 2. 定义完成后再赋值 */</span></span><br><span class="line">&lt;存储类型&gt;  &lt;数据类型&gt;  *&lt;指针变量名&gt;;</span><br><span class="line">&lt;指针变量名&gt; = &lt;地址量&gt;;</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;        <span class="comment">/* 定义一个整型变量 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> *p = <span class="literal">NULL</span>;     <span class="comment">/* 定义一个空指针 */</span></span><br><span class="line"><span class="type">int</span> *p1 = &amp;a;      <span class="comment">/* 定义一个指针变量 p1，同时赋初值，使其指向整型变量 a */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> *p2;           <span class="comment">/* 定义一个指针变量 p2，不进行初始化赋值  */</span></span><br><span class="line">p2 = &amp;a;           <span class="comment">/* 将变量 a 的地址赋给指针变量 p2 ，使其指向整型变量 a */</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）<strong>定义</strong>指针变量时必须<strong>带 * <strong>，给指针变量</strong>赋值</strong>时**不能带 * **。</p>
<p>（2）和普通变量一样，指针变量也可以被多次修改，可以改变指针变量的值，使其指向不同的地址。</p>
<p>（3）地址量必须是一个<strong>地址</strong>，若是普通变量，要加上**取地址 &amp; **符号。</p>
<h3 id="4-3指针变量的引用"><a href="#4-3指针变量的引用" class="headerlink" title="4.3指针变量的引用"></a><font size=3>4.3指针变量的引用</font></h3><ul>
<li>获取数据</li>
</ul>
<p>指针变量存储了数据的地址，通过指针变量能够获得该地址上的数据，格式为：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*&lt;p_name&gt;;</span><br></pre></td></tr></table></figure>

<p>这里的 * 并不是乘号，而是被称为<strong>指针运算符</strong>，用来获取指针变量所指向地址中所存储的数据。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a =<span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p1 = &amp;a;</span><br><span class="line">    <span class="type">int</span> *p2;</span><br><span class="line">    p2 = &amp;a;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, *p1=%d, *p2=%d\n&quot;</span>, a, *p1, *p2);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130130321406.png" alt="image-20220130130321406" style="zoom:80%;" />

<ul>
<li>修改数据</li>
</ul>
<p>通过指针可以直接修改指针所指向变量的数据，使用格式如下</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*&lt;p_name&gt; = value;</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a =<span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p1 = &amp;a;</span><br><span class="line">    <span class="type">int</span> *p2;</span><br><span class="line">    p2 = &amp;a;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, *p1=%d, *p2=%d\n&quot;</span>, a, *p1, *p2);</span><br><span class="line">    *p1 = <span class="number">20</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, *p1=%d, *p2=%d\n&quot;</span>, a, *p1, *p2);</span><br><span class="line">    *p2 = <span class="number">30</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, *p1=%d, *p2=%d\n&quot;</span>, a, *p1, *p2);</span><br><span class="line">    a = <span class="number">40</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, *p1=%d, *p2=%d\n&quot;</span>, a, *p1, *p2);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130141024744.png" alt="image-20220130141024744" style="zoom: 67%;" />

<h3 id="4-4指针和变量在内存中的存放"><a href="#4-4指针和变量在内存中的存放" class="headerlink" title="4.4指针和变量在内存中的存放"></a><font size=3>4.4指针和变量在内存中的存放</font></h3><p>要想更加清楚地理解指针变量，我们可以编写程序来进行测试，好清楚它在内存中的情况。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a =<span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p1 = &amp;a;</span><br><span class="line">    <span class="type">int</span> *p2;</span><br><span class="line">    p2 = &amp;a;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d, &amp;a=%p\n&quot;</span>, a, &amp;a);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;*p1=%d, p1=%p, &amp;p1=%p, &amp;(*p1)=%p\n&quot;</span>, *p1, p1, &amp;p1, &amp;(*p1));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;*p2=%d, p2=%p, &amp;p2=%p, &amp;(*p2)=%p\n&quot;</span>, *p2, p2, &amp;p2, &amp;(*p2));</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130174049078.png" alt="image-20220130174049078" style="zoom: 67%;" />

<p>通过打印指针变量的地址以及普通变量的地址可以得到指针变量和普通变量在内存中的存放如下所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130180725096.png" alt="image-20220130180725096" style="zoom:45%;" />

<h2 id="5-指针占几个字节？"><a href="#5-指针占几个字节？" class="headerlink" title="5.指针占几个字节？"></a><font size=3>5.指针占几个字节？</font></h2><p>从上边对指针的概念的阐述中可以得出，指针也可以称之为地址，而地址的大小就与计算机多少位相关联起来。常见的就是 64 位系统和 32 位系统，接下来就来验证一下不同位数的系统中的指针大小。</p>
<ul>
<li>64 位系统</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a =<span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p = &amp;a;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(p)=%ld\n&quot;</span>,<span class="keyword">sizeof</span>(p));</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130181429628.png" alt="image-20220130181429628" style="zoom:67%;" />

<p>可以看出在 64 位系统下，指针变量 p 占据了 8Byte 的空间，一共就是 64bit 。</p>
<ul>
<li>32 位系统</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a =<span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p = &amp;a;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(p)=%ld\n&quot;</span>,<span class="keyword">sizeof</span>(p));<span class="comment">/* %ld 要改为 %d，否则就会有一个警告 */</span></span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130182415239.png" alt="image-20220130182415239" style="zoom:67%;" />

<p>可以看出在 32 位系统下，指针变量 p 占据了 4Byte 的空间，一共就是 32bit 。</p>
<h2 id="6-指针的运算"><a href="#6-指针的运算" class="headerlink" title="6.指针的运算"></a><font size=3>6.指针的运算</font></h2><p>指针运算是以指针变量所存放的地址量作为运算量而进行的运算，指针运算的<strong>实质就是地址的计算</strong>，指针运算的种类是有限的，它只能进行<strong>赋值运算</strong>、<strong>算术运算</strong>和<strong>关系运算</strong>。</p>
<h3 id="6-1指针的赋值运算"><a href="#6-1指针的赋值运算" class="headerlink" title="6.1指针的赋值运算"></a><font size=3>6.1指针的赋值运算</font></h3><p>见 4.2指针变量的赋值 一节。</p>
<h3 id="6-2指针的算术运算"><a href="#6-2指针的算术运算" class="headerlink" title="6.2指针的算术运算"></a><font size=3>6.2指针的算术运算</font></h3><h4 id="6-2-1算术运算"><a href="#6-2-1算术运算" class="headerlink" title="6.2.1算术运算"></a><font size=3>6.2.1算术运算</font></h4><p>先定义指针变量   px  和 py   。</p>
<table style="font-weight: bold;">
    <tr>
        <td align="center" style="font-weight: bold;">运算符</td>
        <td align="center" style="font-weight: bold;">计算形式</td>
        <td align="center" style="font-weight: bold;">含义</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;">+</td>
        <td align="center" style="font-weight: bold;">px + n</td>
        <td align="left" style="font-weight: bold;">指针向地址大的方向移动 n 个数据<br>实际位置的地址量: (px) + sizeof(px的指向的数据类型) * n</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;">-</td>
        <td align="center" style="font-weight: bold;">px - n</td>
        <td align="left" style="font-weight: bold;">指针向地址小的方向移动 n 个数据<br>实际位置的地址量是: (px) - sizeof(px的指向的数据类型) * n</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;" rowspan="2">++</td>
        <td align="center" style="font-weight: bold;">px++</td>
        <td align="left" style="font-weight: bold;" rowspan="2">指针向地址大的方向移动 n 个数据</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;">++px</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;" rowspan="2">--</td>
        <td align="center" style="font-weight: bold;">px--</td>
        <td align="left" style="font-weight: bold;" rowspan="2">指针向地址小的方向移动 n 个数据</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;">--px</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;">-</td>
        <td align="center" style="font-weight: bold;">px - py</td>
        <td align="left" style="font-weight: bold;">指针 px 与 py之间间隔元素的个数<br>px 和 py 必须是同意数据类型的指针变量，否则两者相减毫无意义。</td>
    </tr>
</table>




<p>【注意】</p>
<p>（1）不同数据类型的两个指针实行加减整数运算是无意义的。</p>
<p>（2） px - py 运算的结果<strong>不是地址量</strong>，而<strong>是一个整数值</strong>，意思就它们相减的结果是<strong>两指针</strong>指向的地址位置<strong>之间相隔数据的个数</strong>，而<strong>不是</strong>两指针持有的<strong>地址值相减</strong>的结果。（<strong>两指针数据类型要一致</strong>）</p>
<h4 id="6-2-2使用实例"><a href="#6-2-2使用实例" class="headerlink" title="6.2.2使用实例"></a><font size=3>6.2.2使用实例</font></h4><p>这里需要提前用一下下边要说的指针与数组的知识，这样通过数组来进行验证，对于指针的算术运算更容易理解些。这里主要验证两指针相减的情况，其他的都比较简单，下一节介绍数组与指针的时候也会经常用到。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">6</span>]  = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    <span class="type">char</span> b[<span class="number">6</span>] = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="type">int</span> *px1 = &amp;a[<span class="number">0</span>];</span><br><span class="line">    <span class="type">int</span> *py1 = &amp;a[<span class="number">5</span>];</span><br><span class="line">    <span class="type">char</span> *px2 = &amp;b[<span class="number">0</span>];</span><br><span class="line">    <span class="type">char</span> *py2 = &amp;b[<span class="number">3</span>];</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">6</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;a[%d]=%d , &amp;a[%d]=%p | b[%d]=%d , &amp;b[%d]=%p\n&quot;</span>, i, a[i], i, &amp;a[i], i, b[i], i, &amp;b[i]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%p - %p = %ld\n&quot;</span>, py1, px1, py1 - px1);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;%p - %p = %ld\n&quot;</span>, py2, px2, py2 - px2);</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220130210001974.png" alt="image-20220130210001974" style="zoom: 67%;" />

<h3 id="6-3指针的关系运算"><a href="#6-3指针的关系运算" class="headerlink" title="6.3指针的关系运算"></a><font size=3>6.3指针的关系运算</font></h3><ul>
<li>关系运算</li>
</ul>
<p>两指针之间的关系运算表示它们指向的<strong>地址位置之间的关系</strong>。指向地址大的指针大于指向地址小的指针。</p>
<table style="font-weight: bold;">
    <tr>
        <td align="center" style="font-weight: bold;">运算符</td>
        <td align="center" style="font-weight: bold;">说明</td>
        <td align="center" style="font-weight: bold;">举例</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> > </td>
        <td align="center" style="font-weight: bold;">大于</td>
        <td align="center" style="font-weight: bold;">px > py</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> < </td>
        <td align="center" style="font-weight: bold;">小于</td>
        <td align="center" style="font-weight: bold;">px < py</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> >= </td>
        <td align="center" style="font-weight: bold;">大于等于</td>
        <td align="center" style="font-weight: bold;">px >= py</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> <= </td>
        <td align="center" style="font-weight: bold;">小于等于</td>
        <td align="center" style="font-weight: bold;">px <= py</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> == </td>
        <td align="center" style="font-weight: bold;">等于</td>
        <td align="center" style="font-weight: bold;">px == py</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold;"> != </td>
        <td align="center" style="font-weight: bold;">不等于</td>
        <td align="center" style="font-weight: bold;">px ！= py</td>
    </tr>
</table>

<p>【注意】</p>
<p>（1）不同数据类型的指针之间的关系运算没有任何意义，指向不同数据区域的数据的两个指针之间的关系运算也没有意义。</p>
<p>（2）指针与一般整数变量之间的关系运算没有意义。但可以和 0(NULL) 进行等于或不等于的关系运算，判断指针是否为空。</p>
<h2 id="7-void-指针"><a href="#7-void-指针" class="headerlink" title="7. void 指针"></a><font size=3>7. void 指针</font></h2><h3 id="7-1-void-指针概念"><a href="#7-1-void-指针概念" class="headerlink" title="7.1 void 指针概念"></a><font size=3>7.1 void 指针概念</font></h3><p> void 指针是一种<strong>不确定数据类型的指针变量</strong>，它可以通过<strong>强制类型转换</strong>让该变量<strong>指向任何数据类型</strong>的变量。由于  void  指针没有特定的类型，因此它可以<strong>指向任何类型的数据</strong>。</p>
<p><strong>【注意】</strong></p>
<p>（1）任何类型的指针都可以直接赋值给 void 指针，而无需进行其他相关的强制类型转换。</p>
<p>（2）要将void 指针 赋给其他类型的指针，则需要强制类型转换。</p>
<h3 id="7-2一般形式"><a href="#7-2一般形式" class="headerlink" title="7.2一般形式"></a><font size=3>7.2一般形式</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> *&lt;指针变量名称&gt;;</span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>在  ANSI C  标准中，对于 void 指针，虽然任何类型的指针都可以直接赋值给  void  指针，但是在<strong>没有强制类型转换之前，不能进行任何指针的算术运算</strong>，这是因为在引用指针目标值时， void * 相当于类型不确定，只知道指针指的起始地址，但是不知道占用的字节数，所以就没有颁发决定以什么单位来进行偏移，就会出现编译错误。</p>
<p>在  GNU  中则允许其进行算术运算，因为在默认情况下， GNU  认为  void * 和 char *  一样，既然是确定的，当然可以进行一些算术操作。</p>
<h3 id="7-3使用规则"><a href="#7-3使用规则" class="headerlink" title="7.3使用规则"></a><font size=3>7.3使用规则</font></h3><p>进行强制类型转换格式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*(&lt;目标数据类型&gt; *)&lt;指针变量名&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>对 void 指针赋值</li>
</ul>
<p> void  指针可以指向任意类型的数据，就是说可以用任意类型的指针对  void 指针赋值。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *a;  <span class="comment">/* 定义一个整型指针变量 a */</span></span><br><span class="line"><span class="type">void</span> *p; <span class="comment">/* 定义一个 void 型指针变量 p */</span></span><br><span class="line">p = a;   <span class="comment">/* 将指针变量 a 指向的地址赋值给 p */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>将 void 指针赋给其他类型指针</li>
</ul>
<p>“空类型”可以包容“有类型”，而“有类型”则不能包容“空类型”，要将  void  指针赋值给其他类型的指针，必须进行强制类型转换。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> *p1;     <span class="comment">/* 定义一个 void 型指针变量 p1 */</span></span><br><span class="line"><span class="type">int</span> *p2;      <span class="comment">/* 定义一个整型指针变量 p2 */</span></span><br><span class="line">p2 = (<span class="type">int</span>*)p1;<span class="comment">/* 将void指针变量p1赋值给int指针变量p2 */</span></span><br></pre></td></tr></table></figure>

<ul>
<li>使用 void 指针</li>
</ul>
<p>必须进行强制类型转换才可以使用。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *a;  <span class="comment">/* 定义一个整型指针变量 a */</span></span><br><span class="line"><span class="type">void</span> *p; <span class="comment">/* 定义一个 void 型指针变量 p */</span></span><br><span class="line"></span><br><span class="line">p = a;   <span class="comment">/* 将指针变量 a 指向的地址赋值给 p */</span></span><br><span class="line"></span><br><span class="line">ptrintf(<span class="string">&quot;*p=%d\n&quot;</span>, *(<span class="type">int</span> *)p);</span><br></pre></td></tr></table></figure>

<h2 id="8-const-指针"><a href="#8-const-指针" class="headerlink" title="8. const 指针"></a><font size=3>8. const 指针</font></h2><h3 id="8-1-const-变量"><a href="#8-1-const-变量" class="headerlink" title="8.1 const 变量"></a><font size=3>8.1 const 变量</font></h3><p> C语言 中，关键字 const 修饰变量，可以使<strong>变量常量化</strong>，这样就使得变量的值不能修改，从而达到保护变量的目的。一般的说明形式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> &lt;数据类型&gt; 变量名 = [&lt;表达式&gt;] ;</span><br></pre></td></tr></table></figure>

<p>那如果说当变量有 const 修饰时，想用指针间接访问变量，指针也要有 const 修饰。那么 const 放在指针声明的什么位置呢？请接着往下看。</p>
<p><strong>【说明】</strong>修饰指针的时候可以简记为左数右指，意思就是const在*号左边，那么指针指向的数据不可改变，但是指针变量可以改变。const在*号右边，表示指针变量指向的数据可以改变，但是指针变量不可改变，需要注意的是c<strong>onst不可以放在指针变量后边</strong>。</p>
<h3 id="8-2常量化指针目标表达式"><a href="#8-2常量化指针目标表达式" class="headerlink" title="8.2常量化指针目标表达式"></a><font size=3>8.2常量化指针目标表达式</font></h3><ul>
<li>一般形式</li>
</ul>
<p>常量化指针目标是<strong>限制通过指针改变其目标的数值</strong> ，但**&lt;指针变量&gt; 存储的地址值可以修改**。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> &lt;数据类型&gt; *&lt;指针变量名称&gt;[= &lt;指针运算表达式&gt;];</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> *p = &amp;a;</span><br></pre></td></tr></table></figure>

<p>【分析】上边定义了两个整型变量 a,b 和一个带有 const 修饰的指针变量，此时指针 p 指向的是 a ，我们<strong>可以</strong>改变 p 中的地址值，即通过 p &#x3D; &b; 使其指向 b ；我们<strong>也可以</strong>通过 *p 来访问相应的目标值，但是，我们<strong>无法</strong>通过 *p &#x3D; 30; 这样的赋值操作来改变目标值，若强行修改则会报以下错误：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">error: assignment of read-only location ‘*p’</span><br></pre></td></tr></table></figure>

<h3 id="8-3常量化指针变量"><a href="#8-3常量化指针变量" class="headerlink" title="8.3常量化指针变量"></a><font size=3>8.3常量化指针变量</font></h3><ul>
<li>一般形式</li>
</ul>
<p>常量化指针变量，使得** &lt;指针变量&gt; 存储的地址值不可被修改<strong>，但是</strong>可以通过 *&lt;指针变量名称&gt; 修改指针所指向变量的值**。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;数据类型&gt; * <span class="type">const</span> &lt;指针变量名&gt; = &lt;指针运算表达式&gt; ; </span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line"><span class="type">int</span> * <span class="type">const</span> p = &amp;a;</span><br></pre></td></tr></table></figure>

<p>【分析】上边定义了两个整型变量 a，b 和一个带有 const 修饰的指针变量，此时指针 p 指向的是 a ，我们<strong>无法</strong>改变 p 中的地址值，即通过 p &#x3D; &b; 使其指向 b ，若强行修改则会报以下错误：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">error: assignment of read-only variable ‘p’</span><br></pre></td></tr></table></figure>

<p>我们<strong>可以</strong>通过 *p 来访问相应的目标值，我们<strong>也可以</strong>通过 *p &#x3D; 30; 这样的赋值操作来改变目标值。</p>
<h3 id="8-4常量化指针变量及其目标表达式"><a href="#8-4常量化指针变量及其目标表达式" class="headerlink" title="8.4常量化指针变量及其目标表达式  "></a><font size=3>8.4常量化指针变量及其目标表达式  </font></h3><ul>
<li>一般形式</li>
</ul>
<p>常量化指针变量及其目标表达式，使得<strong>既不可以修改&lt;指针变量&gt;的地址</strong>，也<em><em>不可以通过</em>&lt;指针变量名称&gt;修改指针所指向变量的值</em>*。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">const</span> &lt;数据类型&gt; *<span class="type">const</span> &lt;指针变量名&gt; = &lt;指针运算表达式&gt; ; </span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line"><span class="type">const</span> <span class="type">int</span> * <span class="type">const</span> p = &amp;a;</span><br></pre></td></tr></table></figure>

<p>分析：上边定义了两个整型变量 a,b 和一个带有 const 修饰的指针变量，此时指针 p 指向的是 a ，我们<strong>无法</strong>改变 p 中的地址值，即通过 p &#x3D; &b; 使其指向 b ；我们<strong>可以</strong>通过 *p 来访问相应的目标值，但是，我们<strong>无法</strong>通过 *p &#x3D; 30; 这样的赋值操作来改变目标值。若强行修改地址值或者目标值，则会报以下错误：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">error: assignment of read-only variable ‘p’  <span class="comment">/* 通过p=&amp;b;修改存储的地址值 */</span></span><br><span class="line">error: assignment of read-only location ‘*p’ <span class="comment">/* 通过*p=30;修改目标值 */</span></span><br></pre></td></tr></table></figure>

<h1 id="二、指针与数组"><a href="#二、指针与数组" class="headerlink" title="二、指针与数组"></a><font size=3>二、指针与数组</font></h1><h2 id="1-指针与一维数组"><a href="#1-指针与一维数组" class="headerlink" title="1.指针与一维数组"></a><font size=3>1.指针与一维数组</font></h2><h3 id="1-1数组的指针"><a href="#1-1数组的指针" class="headerlink" title="1.1数组的指针"></a><font size=3>1.1数组的指针</font></h3><p>数组元素的地址是指数组元素在内存中的起始地址，可以由各个元素加上取地址符号 &amp; 组成，而数组名就代表了数组的起始地址。</p>
<p><strong>数组指针</strong>就是指向<strong>数组起始地址</strong>的指针，其本质就是指针，一维数组名可以看做是一维数组的指针（但是一维数组名不能像指针变量那样做自 ++ 和 – 运算）</p>
<h3 id="1-2数组元素的表示"><a href="#1-2数组元素的表示" class="headerlink" title="1.2数组元素的表示"></a><font size=3>1.2数组元素的表示</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">3</span>]  = &#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;;</span><br><span class="line">    <span class="type">int</span> *p = a;</span><br><span class="line">    <span class="type">int</span> i;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a[i]\t p[i]\t *(a+i)\t *(p+i)\t|&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\t   &amp;a[i]\t    &amp;p[i]\t     a+i\t     p+i \n&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;---------------------------------------------------&quot;</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;---------------------------------------------------\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">3</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot; %d\t   %d\t    %d\t    %d\t|&quot;</span>, a[i], p[i], *(a+i), *(p+i));</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\t%p\t%p\t%p\t%p\n&quot;</span>, &amp;a[i], &amp;p[i], a+i, p+i);</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204134546751.png" alt="image-20220204134546751" style="zoom: 67%;" />

<p>从上边实例可以看出：</p>
<ul>
<li><p>a[i]，p[i]，*(p+i)，*(a+i)四者等价，均可表 示数组元素；</p>
</li>
<li><p>&amp;a[i]，&amp;p[i]，p+i, a+i四者等价，均可表示相应数组元素地址；</p>
</li>
</ul>
<table>
    <tr>
        <td align="center" colspan="4">元素表示</td>
        <td align="center" colspan="4">元素地址表示</td>
    </tr>
    <tr>
        <td align="center" colspan="4" style="font-weight: bold; color:red;">a[i] &hArr; p[i] &hArr; *(p+i) &hArr; *(a+i)</td>
        <td align="center" colspan="4" style="font-weight: bold; color:red;">&a[i] &hArr; &p[i] &hArr; p+i &hArr; a+i</td>
    </tr>
    <tr>
        <td align="center">a[0]</td>
        <td align="center">p[0]</td>
        <td align="center">*(p+0)</td>
        <td align="center">*(a+0)</td>
        <td align="center">&a[0]</td>
        <td align="center">&p[0]</td>
        <td align="center">p+0</td>
        <td align="center">a+0</td>
    </tr>
    <tr>
        <td align="center">a[1]</td>
        <td align="center">p[1]</td>
        <td align="center">*(p+1)</td>
        <td align="center">*(a+1)</td>
        <td align="center">&a[1]</td>
        <td align="center">&p[1]</td>
        <td align="center">p+1</td>
        <td align="center">a+1</td>
    </tr>
    <tr>
        <td align="center">a[2]</td>
        <td align="center">p[2]</td>
        <td align="center">*(p+2)</td>
        <td align="center">*(a+2)</td>
        <td align="center">&a[2]</td>
        <td align="center">&p[2]</td>
        <td align="center">p+2</td>
        <td align="center">a+2</td>
    </tr>
</table>


<p>【注意】</p>
<p>（1）指针变量和数组在访问数组中元素时，一定条件下其使用方法具有相同的形式，因为<strong>指针变量和数组名都是地址量</strong>。</p>
<p>（2）指针变量和数组的指针（或叫数组名）在<strong>本质上不同</strong>，<strong>指针变量是地址变量</strong>，而<strong>数组的指针是地址常量</strong>。例如 p++, p– (正确); a++, a–(错误) 。</p>
<h2 id="2-指针与二维数组"><a href="#2-指针与二维数组" class="headerlink" title="2.指针与二维数组"></a><font size=3>2.指针与二维数组</font></h2><h3 id="2-1列指针遍历二维数组"><a href="#2-1列指针遍历二维数组" class="headerlink" title="2.1列指针遍历二维数组"></a><font size=3>2.1列指针遍历二维数组</font></h3><p>在 C语言中 二维数组的元素连续存储，按行优先存储，所以自然可以使用一级指针来进行访问。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]  = &#123;&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;, &#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> *p = a[<span class="number">0</span>]; <span class="comment">/* 不能直接写 int *p = a;这样类型不匹配，会有警告 */</span></span><br><span class="line">    <span class="type">int</span> i, j;</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>;i &lt; <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(j = <span class="number">0</span>;j &lt; <span class="number">3</span>; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;a[%d][%d]=%d,&amp;a[%d][%d]=%p | &quot;</span>,i, j, a[i][j], i, j, &amp;a[i][j]);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;*(p+%d)=%d,p+%d=%p\n&quot;</span>,i * <span class="number">3</span> + j, *(p + i * <span class="number">3</span> + j), i * <span class="number">3</span> + j, p + i * <span class="number">3</span> + j);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204155825156.png" alt="image-20220204155825156" style="zoom: 67%;" />

<p>从例子中可以看出，一级指针 p 移动了 6 个数，到达 p + 5 时，将 a[2][3] 中的 6 个数访问完毕，相当于移动了 6 列，因此一级指针 p 也叫作<strong>列指针</strong>。</p>
<p>【注意】</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *p= a[<span class="number">0</span>];  <span class="comment">/* 列指针的定义法 */</span> </span><br></pre></td></tr></table></figure>



<h3 id="2-2行指针遍历二维数组"><a href="#2-2行指针遍历二维数组" class="headerlink" title="2.2行指针遍历二维数组"></a><font size=3>2.2行指针遍历二维数组</font></h3><ul>
<li>行指针的概念</li>
</ul>
<p>在学习二维数组时，我们就知道二维数组可以看做是多个一维数组，对于 a[2][3] 来说，有如下表格中理解：</p>
<table>
    <tr>
        <td align="center" colspan="3">行名(代表了地址)</td>
        <td align="center" colspan="3">每行元素</td>
    </tr>
    <tr>
        <td align="center">a</td>
        <td align="center">a[0]</td>
        <td align="center">&a[0][0]</td>
        <td align="center">a[0][0]</td>
        <td align="center">a[0][1]</td>
        <td align="center">a[0][2]</td>
    </tr>
    <tr>
        <td align="center">a + 1</td>
        <td align="center">a[1]</td>
        <td align="center">&a[1][0]</td>
        <td align="center">a[1][0]</td>
        <td align="center">a[1][1]</td>
        <td align="center">a[1][2]</td>
    </tr>
</table>


<p>在指针与一维数组中我们知道** a[i], p[i], *(p+i), *(a+i)四者等价;&amp;a[i], &amp;p[i], p+i, a+i四者等价 **，所以类似的有：</p>
<table>
    <tr>
        <td align="center" colspan="4">行名(代表了地址)</td>
        <td align="center" colspan="3">每行元素地址</td>
    </tr>
    <tr>
        <td align="center">第1行</td>
        <td align="center">a</td>
        <td align="center">a[0] &hArr; *(a + 0)</td>
        <td align="center">&a[0][0]</td>
        <td align="center">a[0][0]</td>
        <td align="center">a[0][1]</td>
        <td align="center">a[0][2]</td>
    </tr>
    <tr>
        <td align="center">第2行</td>
        <td align="center">a + 1</td>
        <td align="center">a[1] &hArr; *(a + 1)</td>
        <td align="center">&a[1][0]</td>
        <td align="center">a[1][0]</td>
        <td align="center">a[1][1]</td>
        <td align="center">a[1][2]</td>
    </tr>
</table>


<p>所以，第2行第2列的元素就可以表示为:</p>
<table>
    <tr>
        <td align="center">a[1][1] &hArr; *(&a[1][0] + 1) &hArr; *(a[1] + 1) &hArr; *(*(a + 1) + 1)</td>
    </tr>
</table>


<p>上边的 a[0]、a[1] 代表了行地址，但是与一维数组名一样，是地址常量。指针变量存储的是地址，那么存储行地址的指针变量，被称之为<strong>行指针</strong>。</p>
<ul>
<li>行指针的定义</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;存储类型&gt; &lt;数据类型&gt; (*&lt;指针变量名&gt;)[表达式] ; </span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）存储类型指的是 auto, register, static, extern ，若省略，则默认为 auto 。</p>
<p>（2）赋值时要注意定义为行指针的指针变量才可以直接将数组名赋值给指针变量，否则会有警告。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>]; </span><br><span class="line"><span class="type">int</span> *q = a;       <span class="comment">/* 会报警告*/</span></span><br><span class="line"><span class="type">int</span> (*p)[<span class="number">3</span>] = a;  <span class="comment">/* 正确方式*/</span></span><br></pre></td></tr></table></figure>

<ul>
<li>行指针使用</li>
</ul>
<p>若有 int a[2][3]; int (*p) &#x3D; a;  则有</p>
<table>
    <tr>
        <td align="center" style="font-weight: bold; color:red;">a[i][j] &hArr; *(&a[i][0] + j) &hArr; *(a[i] + j) &hArr; *(*(a + i) + j)</td>
    </tr>
    <tr>
        <td align="center" style="font-weight: bold; color:blue;">p[i][j] &hArr; *(&p[i][0] + j) &hArr; *(p[i] + j) &hArr; *(*(p + i) + j)</td>
    </tr>
</table>


<h3 id="2-3行指针与列指针"><a href="#2-3行指针与列指针" class="headerlink" title="2.3行指针与列指针"></a><font size=3>2.3行指针与列指针</font></h3><p>对于如下定义的二维数组：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a[<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;, &#123;<span class="number">3</span>, <span class="number">4</span>,<span class="number">5</span> &#125;&#125;; </span><br></pre></td></tr></table></figure>

<p>有如下说明</p>
<table>
    <tr>
        <td align="center">指针类型</td>
        <td align="center">表示形式</td>
        <td align="center">说明</td>
    </tr>
    <tr>
        <td align="center" rowspan="2">行指针</td>
        <td align="center">a 或 a + 0</td>
        <td align="left">指向第0行</td>
    </tr>
    <tr>
        <td align="center">a + 1</td>
        <td align="left">指向第1行</td>
    </tr>
    <tr>
        <td align="center" rowspan="6">列指针</td>
        <td align="center">a[0]</td>
        <td align="left">是一维数组的名称，也是整个数组的首地址。<br>第0行第1个元素(a[0][0])的地址。</td>
    </tr>
    <tr>
        <td align="center">a[0] + 1</td>
        <td align="left">第0行第2个元素(a[0][1])的地址</td>
    </tr>
    <tr>
        <td align="center">a[0] + 2</td>
        <td align="left">第0行第3个元素(a[0][2])的地址</td>
    </tr>
    <tr>
        <td align="center">a[1]</td>
        <td align="left">第1行第1个元素(a[1][0])的地址</td>
    </tr>
    <tr>
        <td align="center">a[1] + 1</td>
        <td align="left">第1行第2个元素(a[1][1])的地址</td>
    </tr>
    <tr>
        <td align="center">a[1] + 2</td>
        <td align="left">第1行第3个元素(a[1][2])的地址</td>
    </tr>
</table>


<p>可以将<strong>列指针理解为行指针的具体元素，行指针理解为列指针的地址。</strong>那么他们的具体关系就可以这样表示：</p>
<table>
    <tr>
        <td align="center">*行指针 &rArr; 列指针</td>
    </tr>
    <tr>
        <td align="center">&列指针 &rArr; 行指针</td>
    </tr>
</table>


<p>一般来说我们都是定义行指针，再转换为列指针进而访问数组具体元素，所以这里只写一个行指针转换为列指针的列子，列指针转行指针应该属于一个逆运算，但是意义好像不大，也基本没有遇到过。</p>
<table>
    <tr>
        <td align="center">行指针&rArr;列指针</td>
        <td align="center">列指针等价表示</td>
        <td align="center">指向内容</td>
        <td align="center">指向内容的等价表示</td>
        <td align="center">数组表示</td>
    </tr>
    <tr>
        <td align="center">a &rArr; *a</td>
        <td align="center">a[0]</td>
        <td align="center">*a[0]</td>
        <td align="center">*(*a)</td>
        <td align="center">a[0][0]</td>
    </tr>
    <tr>
        <td align="center">a+1 &rArr; *(a+1)</td>
        <td align="center">a[1]</td>
        <td align="center">*a[1]</td>
        <td align="center">*(*(a+1))</td>
        <td align="center">a[1][0]</td>
    </tr>
</table>


<h2 id="3-指针数组"><a href="#3-指针数组" class="headerlink" title="3.指针数组"></a><font size=3>3.指针数组</font></h2><h3 id="3-1指针数组的定义"><a href="#3-1指针数组的定义" class="headerlink" title="3.1指针数组的定义"></a><font size=3>3.1指针数组的定义</font></h3><p><strong>指针数组</strong>是指由若干个具有<strong>相同存储类型和数据类型</strong>的<strong>指针变量</strong>构成的<strong>集合</strong>，也就是说这个数组中的每个元素都是同类型的指针变量。</p>
<p>一般声明形式：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;存储类型&gt;  &lt;数据类型&gt;  *&lt;指针数组名&gt;[&lt;指针数组大小&gt;];</span><br></pre></td></tr></table></figure>

<p>【注意】<strong>指针数组名</strong>表示该指针数组的<strong>起始地址</strong>。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *pa[<span class="number">2</span>]; <span class="comment">/* 定义了一个长度为 2 的 int 型指针数组*/</span></span><br><span class="line"><span class="type">char</span> *pb[<span class="number">6</span>];<span class="comment">/* 定义了一个长度为 6 的 char 型指针数组*/</span></span><br></pre></td></tr></table></figure>

<p>但是其实写成下边这种形式可能会更好理解一些，看个人吧。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> * pa[<span class="number">2</span>]; <span class="comment">/* 定义了一个长度为 2 的 int 型指针数组*/</span></span><br><span class="line"><span class="type">char</span> * pb[<span class="number">6</span>];<span class="comment">/* 定义了一个长度为 6 的 char 型指针数组*/</span></span><br></pre></td></tr></table></figure>

<p>可以这样理解，对于 int * pa[2]; 来说，是声明了一个 int 类型的一维数组 pa[2] ，它包含了两个元素，每个元素都是一个 int 类型的指针。</p>
<h3 id="3-2指针数组的初始化"><a href="#3-2指针数组的初始化" class="headerlink" title="3.2指针数组的初始化"></a><font size=3>3.2指针数组的初始化</font></h3><table>
    <tr>
        <td align="center" width=300px>初始化写法</td>
        <td align="center" width=300px>等价写法(赋值)</td>
    </tr>
    <tr>
        <td align="left">int a = 10;<br>
            int b = 20;<br>
            int * p[2] = {&a, &b};</td>
        <td align="left">int a = 10;<br>
            int b = 20;<br>
            int * p[2];<br>
            p[0] = &a;<br>
            p[1] = &b;<br>
        </td>
    </tr>
</table>



<h3 id="3-3指针数组的存储空间"><a href="#3-3指针数组的存储空间" class="headerlink" title="3.3指针数组的存储空间"></a><font size=3>3.3指针数组的存储空间</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">    <span class="type">int</span> * p[<span class="number">2</span>] = &#123;&amp;a, &amp;b&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;p=%p,&amp;p=%p,&amp;p[0]=%p,&amp;p[1]=%p\n&quot;</span>, p, &amp;p, &amp;p[<span class="number">0</span>],&amp;p[<span class="number">1</span>]);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d,&amp;a=%p,p[0]=%p,*p[0]=%d\n&quot;</span>, a, &amp;a, p[<span class="number">0</span>], *p[<span class="number">0</span>]);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;b=%d,&amp;b=%p,p[1]=%p,*p[1]=%d\n&quot;</span>, b, &amp;b, p[<span class="number">1</span>], *p[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204190231704.png" alt="image-20220204190231704" style="zoom:67%;" />

<p>由程序结果可知变量在内存中存储如下图所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204195558425.png" alt="image-20220204195558425" style="zoom: 50%;" />

<p>【注意】图中红色的两个地址之间是有 8Byte 的空间，类型明明是 int ，应该是 4 个字节才对吧，又怎么会这样呢？在介绍指针的的时候说过，<strong>指针变量所占空间是固定的，在 32 位系统中就是 4 个字节，而在 64 位系统中则是 8 个字节</strong>，由于这里是 64 位系统，所以指针数组名加 1 ，就会移动 8 个字节。</p>
<h3 id="3-4指针数组的数组名"><a href="#3-4指针数组的数组名" class="headerlink" title="3.4指针数组的数组名"></a><font size=3>3.4指针数组的数组名</font></h3><p>对于指针数组的数组名，它代表了指针数组的起始地址。由于数组元素已经是指针了，而数组名又是数组首元素的地址，因此指针数组名就是指针数组中<strong>首个指针元素的地址</strong>，所以指针数组名是一个<strong>多级指针</strong>，具体来说就是一个二级指针。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">    <span class="type">int</span> * p[<span class="number">2</span>] = &#123;&amp;a, &amp;b&#125;;</span><br><span class="line">    <span class="type">int</span> * *q;</span><br><span class="line"></span><br><span class="line">    q = p;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;sizeof(p)=%ld,sizeof(q)=%ld\n&quot;</span>,<span class="keyword">sizeof</span>(p), <span class="keyword">sizeof</span>(q));</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;q=%p,&amp;q=%p,&amp;q[0]=%p,&amp;q[1]=%p\n&quot;</span>, q, &amp;q, &amp;p[<span class="number">0</span>],&amp;q[<span class="number">1</span>]);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;p=%p,&amp;p=%p,&amp;p[0]=%p,&amp;p[1]=%p\n&quot;</span>, p, &amp;p, &amp;p[<span class="number">0</span>],&amp;p[<span class="number">1</span>]);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d,&amp;a=%p,p[0]=%p,*p[0]=%d,**p=%d\n&quot;</span>, a, &amp;a, p[<span class="number">0</span>], *p[<span class="number">0</span>], **p);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;b=%d,&amp;b=%p,p[1]=%p,*p[1]=%d,**(p+1)=%d\n&quot;</span>, b, &amp;b, p[<span class="number">1</span>], *p[<span class="number">1</span>], **(p+<span class="number">1</span>));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204203431783.png" alt="image-20220204203431783" style="zoom:67%;" />

<p>【注意】</p>
<p>（1）从另一个方面理解，在一维数组中我们知道** a[i], p[i], *(p+i), *(a+i)四者等价 *<em>，所以直接将  p[i]替换为</em>(p+i) 那么也可以得到上边的结果。</p>
<p>（2）通过 sizeof(指针数组名) 可以求得指针数组占据的总空间，而每个元素所占空间与系统相关。</p>
<h3 id="3-5指针数组与二维数组"><a href="#3-5指针数组与二维数组" class="headerlink" title="3.5指针数组与二维数组"></a><font size=3>3.5指针数组与二维数组</font></h3><p>考虑到二维数组可以通过行指针来访问，这样的话通过指针数组来存储二维数组的行指针，那么这样就可以通过指针数组遍历二维数组了。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204204857551.png" alt="image-20220204204857551" style="zoom:67%;" />

<ul>
<li>通过指针数组遍历二维数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a [<span class="number">2</span>][<span class="number">3</span>] = &#123;&#123;<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>&#125;, &#123;<span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;&#125;;</span><br><span class="line">    <span class="type">int</span> * p[<span class="number">2</span>] = &#123;a[<span class="number">0</span>], a[<span class="number">1</span>]&#125;;</span><br><span class="line">    <span class="type">int</span> i, j;</span><br><span class="line">    <span class="keyword">for</span>(i = <span class="number">0</span>; i &lt; <span class="number">2</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(j = <span class="number">0</span>; j &lt; <span class="number">3</span>; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;a[%d][%d]=%d,p[%d][%d]=%d,&quot;</span>, i, j, a[i][j], i, j, p[i][j]);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;*(p[%d] + %d)=%d,*(*(p + %d) + %d)=%d\n&quot;</span>, i, j, *(p[i] + j), i, j, *(*(p + i) + j));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220204210115901.png" alt="image-20220204210115901" style="zoom:67%;" />

<h1 id="三、指针与字符串"><a href="#三、指针与字符串" class="headerlink" title="三、指针与字符串"></a><font size=3>三、指针与字符串</font></h1><h2 id="1-字符指针变量的概念"><a href="#1-字符指针变量的概念" class="headerlink" title="1.字符指针变量的概念"></a><font size=3>1.字符指针变量的概念</font></h2><p> C语言 通过使用字符数 组来处理字符串， char 数据类型的指针变量称为<strong>字符指针变量</strong>。字符指针变量与字符数组有着密切关系，它也被用来处理字符串。</p>
<h2 id="2-字符指针变量的赋值"><a href="#2-字符指针变量的赋值" class="headerlink" title="2.字符指针变量的赋值"></a><font size=3>2.字符指针变量的赋值</font></h2><ul>
<li>指向字符数组</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> str[] = <span class="string">&quot;strings&quot;</span>;</span><br><span class="line"><span class="type">char</span> *p = str;</span><br></pre></td></tr></table></figure>

<p>这样的赋值方式是将字符指针变量 p 指向数组 str 的首地址，即将字符串的首地址赋予指针，并不是把该字符串复制到指针中。</p>
<ul>
<li>指向字符串常量</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> *p = <span class="string">&quot;string&quot;</span>;</span><br></pre></td></tr></table></figure>

<p>这样的赋值方式是将一个<strong>字符串常量</strong>的首地址赋给指针变量 p ，这样的赋值方式会使指针指向对象的值不能被修改，例如 *p &#x3D;  ‘S’; 这将导致程序报错，原因就在于字符串常量不能被修改。</p>
<h2 id="3-字符串打印"><a href="#3-字符串打印" class="headerlink" title="3.字符串打印"></a><font size=3>3.字符串打印</font></h2><p>由于指针变量指向的是字符串的首地址，所以我们可以这样来打印指针变量指向的字符串。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">char</span> str[] = <span class="string">&quot;strings&quot;</span>;</span><br><span class="line">    <span class="type">char</span> *p = str;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str[]=%s,p=%s\n&quot;</span>, str, p);</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">str[]=strings,p=strings</span><br></pre></td></tr></table></figure>

<h1 id="四、多级指针"><a href="#四、多级指针" class="headerlink" title="四、多级指针"></a><font size=3>四、多级指针</font></h1><h2 id="1-多级指针的概念"><a href="#1-多级指针的概念" class="headerlink" title="1.多级指针的概念"></a><font size=3>1.多级指针的概念</font></h2><ul>
<li><p>把一个<strong>指向指针变量</strong>的<strong>指针变量</strong>，称为多级指针变量。</p>
</li>
<li><p>对于<strong>指向处理数据</strong>的<strong>指针变量</strong>称为一级指针变量，简称一级指针。</p>
</li>
<li><p>把<strong>指向一级指针变量</strong>的<strong>指针变量</strong>称为二级指针变量，简称二级指针。</p>
</li>
</ul>
<h2 id="2-二-多-级指针的声明"><a href="#2-二-多-级指针的声明" class="headerlink" title="2.二(多)级指针的声明"></a><font size=3>2.二(多)级指针的声明</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;存储类型&gt; &lt;数据类型&gt; ** &lt;指针名&gt;; <span class="comment">/* 几级指针就写几个* 号*/</span></span><br></pre></td></tr></table></figure>

<p>【注意】</p>
<p>（1）指针变量也是一种变量，也会占用存储空间，也可以使用 &amp; 获取它的地址。</p>
<p>（2） C语言 不限制指针的级数，**每增加一级指针，在定义指针变量时就得增加一个星号 * **。实际开发中会经常使用一级指针和二级指针，几乎用不到高级指针。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;  <span class="comment">/* 定义了一个整型变量 a */</span></span><br><span class="line"><span class="type">int</span> *p1;     <span class="comment">/* 定义了一个一级整型指针变量 p1 */</span></span><br><span class="line"><span class="type">int</span> **p2;    <span class="comment">/* 定义了一个二级整型指针变量 p2 */</span></span><br><span class="line"><span class="type">int</span> ***p3;   <span class="comment">/* 定义了一个三级整型指针变量 p3 */</span></span><br><span class="line"><span class="type">int</span> ****p4;  <span class="comment">/* 定义了一个四级整型指针变量 p4 */</span></span><br></pre></td></tr></table></figure>

<h2 id="3-二-多-级指针的初始化"><a href="#3-二-多-级指针的初始化" class="headerlink" title="3.二(多)级指针的初始化"></a><font size=3>3.二(多)级指针的初始化</font></h2><table>
    <tr>
        <td align="center" width=300px>初始化写法</td>
        <td align="center" width=300px>等价写法(赋值)</td>
    </tr>
    <tr>
        <td align="left">
            int a = 10;<br>
            int *p1 = &a;<br>
            int **p2 = &p1;<br>
            int ***p3 = &p2;<br>
            int ****p4 = &p3;
        </td>
        <td align="left">
            int a = 10;<br>
            int *p1;<br>
            int **p2;<br>
            int ***p3;<br>
            int ****p4;<br>
            p1 = &a;<br>
            p2 = &p1;<br>
            p3 = &p2;<br>
            p4 = &p3;<br>
        </td>
    </tr>
</table>



<p>【注意】指针变量的初始化或者赋值，后边一定要是<strong>地址</strong>。</p>
<p>【说明】</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206154923623.png" alt="image-20220206154923623" style="zoom:50%;" />

<p>上边初始化的部分，定义了一个整型变量 a ，然后定义了一个一级指针变量 p1 ，指针变量 p1 中存放的数据是整型变量 a 的地址；又定义了一个二级指针变量 p2 ， p2 中存放的是 p1 的地址；又定义了一个指针变量 p3 ，里边存放的是 p2 的地址；再后面又定义了一个四级指针变量 p4 ，里边存放的是 p3 的地址。</p>
<h2 id="4-二-多-级指针的引用"><a href="#4-二-多-级指针的引用" class="headerlink" title="4.二(多)级指针的引用"></a><font size=3>4.二(多)级指针的引用</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">** &lt;指针名&gt;; <span class="comment">/* 几级指针就写几个* 号*/</span></span><br></pre></td></tr></table></figure>

<p>通过上一节的多级指针初始化的分析可知，多级指针存放的都是地址，而 * 号可以取出地址中的数据，详细的使用情况可看下一节多级指针的存储空间的介绍。</p>
<h2 id="5-多级指针的存储空间"><a href="#5-多级指针的存储空间" class="headerlink" title="5.多级指针的存储空间"></a><font size=3>5.多级指针的存储空间</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="type">int</span> *p1 = &amp;a;</span><br><span class="line">    <span class="type">int</span> **p2 = &amp;p1;</span><br><span class="line">    <span class="type">int</span> ***p3 = &amp;p2;</span><br><span class="line">    <span class="type">int</span> ****p4 = &amp;p3;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d,&amp;a=%p\n&quot;</span>, a, &amp;a);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;*p1=%d,p1=%p,&amp;p1=%p\n&quot;</span>, *p1, p1, &amp;p1);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;**p2=%d,*p2=%p,p2=%p,&amp;p2=%p\n&quot;</span>, **p2, *p2, p2, &amp;p2);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;***p3=%d,**p3=%p,*p3=%p,p3=%p,&amp;p3=%p\n&quot;</span>, ***p3, **p3, *p3, p3, &amp;p3);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;****p4=%d,***p4=%p,**p4=%p,*p4=%p,p4=%p,&amp;p4=%p\n&quot;</span>, ****p4, ***p4, **p4, *p4, p4, &amp;p4);</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206165514337.png" alt="image-20220206165514337" style="zoom:67%;" />

<p>由上图分析可得各级指针在内存中情况如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220206172020146.png" alt="image-20220206172020146" style="zoom:67%;" />

<p>【注意】</p>
<p>（1）相邻红色的地址编号相差都为 8 ，这是因为二级、三级和四级指针中存储的都是指针变量，而在 64 位系统中指针变量所占空间为 8 个字节，所以就出现了上边的情况。</p>
<p>（2） * 号是自右向左结合，所以引用多级指针的时候的时候可以这样理解：<br>$$<br>\begin{flalign}<br>****p4 &#x3D; ***(*p4) &#x3D; ***p3 &#x3D; **(*p3) &#x3D; **p2 &#x3D; *(*p2) &#x3D; *p1 &#x3D; 10 \<br>***p3 &#x3D; **(*p3) &#x3D; **p2 &#x3D; *(*p2) &#x3D; *p1 &#x3D; 10 \<br>**p2 &#x3D; *(*p2) &#x3D; *p1 &#x3D; 10 \<br>*p1 &#x3D; 10<br>\end{flalign}<br>$$</p>
<h2 id="6-多级指针的运算"><a href="#6-多级指针的运算" class="headerlink" title="6.多级指针的运算"></a><font size=3>6.多级指针的运算</font></h2><p>总的来说，多级指针也是指针，符合一级指针的运算规则。指针变量加1，是向地址大的方向移动一个目标数据。类似，多级指针运算也是以其<strong>目标变量</strong>为单位进行偏移。</p>
<p>比如， int **p;p++; 移动一个 int * 变量所占的内存空间。再比如 int ***p;p++ 移动一个 int ** 所占的内存空间。</p>
<p>其实多级指针中存放的都是指针变量，除了一级指针，多级指针移动时，移动的都是一个指针变量的大小，在 32 位系统中是 4 个字节，在 64 位系统中是 8 个字节。</p>
<h1 id="五、函数指针"><a href="#五、函数指针" class="headerlink" title="五、函数指针"></a><font size=3>五、函数指针</font></h1><p>为了汇总，这里需要使用函数的概念，函数的详情可以看下一章。</p>
<h2 id="1-函数指针的定义"><a href="#1-函数指针的定义" class="headerlink" title="1.函数指针的定义"></a><font size=3>1.函数指针的定义</font></h2><p>如果在程序中定义了一个函数，那么在编译时系统就会为这个函数代码<strong>分配一段存储空间，这段存储空间的首地址称为这个函数的地址</strong>。<strong>函数名表示的就是这个地址</strong>。通过指针的概念，我们知道既然是地址，那么就可以定义一个指针变量来存放，这个<strong>指针变量</strong>就叫作函数指针变量，简称<strong>函数指针</strong>。</p>
<h2 id="2-函数指针的声明"><a href="#2-函数指针的声明" class="headerlink" title="2.函数指针的声明"></a><font size=3>2.函数指针的声明</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;数据类型&gt; (*&lt;函数指针名称&gt;) (&lt;参数说明列表&gt;);</span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=200px>数据类型</td>
        <td align="left">函数指针所指向的函数的返回值类型
</td>
    </tr>
    <tr>
        <td align="center" width=200px>（*<函数指针名称>）</td>
        <td align="left">*说明为指针()不可缺省，表明为函数的指针
</td>
    </tr>
    <tr>
        <td align="center" width=200px>参数说明列表</td>
        <td align="left">应该与函数指针所指向的函数的形参说明保持一致
</td>
    </tr>
</table>


<p>【注意】<strong>指向函数的指针变量没有  ++  和  –  运算</strong>。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*p)(<span class="type">int</span>, <span class="type">int</span>);</span><br></pre></td></tr></table></figure>

<p>这个语句定义了一个指向函数的指针变量  p :</p>
<p>（1）它是一个<strong>指针变量</strong>，所以要有一个 * ，即（ *p ）；</p>
<p>（2）前面的  int  表示这个指针变量可以<strong>指向返回值类型为  int  型的函数</strong>；</p>
<p>（3）括号中的两个  int  表示这个指针变量可以<strong>指向有两个参数且都是  int  型的函数</strong>。</p>
<p>所以总体来说，这个语句的意思就是：定义了一个指针变量  p ，该指针变量可以指向返回值类型为  int  型，且有两个整型参数的函数。 p  的类型为  int (*)(int，int) 。</p>
<h2 id="3-函数指针的使用"><a href="#3-函数指针的使用" class="headerlink" title="3.函数指针的使用"></a><font size=3>3.函数指针的使用</font></h2><h3 id="3-1函数指针初始化"><a href="#3-1函数指针初始化" class="headerlink" title="3.1函数指针初始化"></a><font size=3>3.1函数指针初始化</font></h3><p>前边需要有一个函数的声明：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>; <span class="comment">/* 函数声明 */</span></span><br></pre></td></tr></table></figure>

<table>
    <tr>
        <td align="center" width=300px>初始化写法</td>
        <td align="center" width=300px>等价写法(赋值)</td>
    </tr>
    <tr>
        <td align="left">
            int (*p)(int, int) = sum;
        </td>
        <td align="left">
            int (*p)(int, int);<br>
            p = sum;
        </td>
    </tr>
</table>


<h3 id="3-2函数指针的调用"><a href="#3-2函数指针的调用" class="headerlink" title="3.2函数指针的调用"></a><font size=3>3.2函数指针的调用</font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[ &lt;变量名&gt; = ] (*&lt;函数指针名称&gt;)(&lt;实际传入参数说明列表&gt;);</span><br></pre></td></tr></table></figure>

<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* 提前定义的函数 */</span></span><br><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>; <span class="comment">/* 函数声明 */</span></span><br><span class="line"><span class="comment">/* --------------------------------------------------  */</span></span><br><span class="line"><span class="comment">/* 主程序中函数指针的操作 */</span></span><br><span class="line"><span class="type">int</span> (*p)(<span class="type">int</span>, <span class="type">int</span>) = sum;  <span class="comment">/* 定义一个函数指针 */</span></span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;a=%d,b=%d,(*p)(a, b)=%d\n&quot;</span>, a, b, (*p)(a, b));</span><br><span class="line"></span><br><span class="line"><span class="comment">/* --------------------------------------------------  */</span></span><br><span class="line"><span class="comment">/* 或者 */</span></span><br><span class="line"><span class="type">int</span> c = <span class="number">0</span>;</span><br><span class="line"><span class="type">int</span> (*p)(<span class="type">int</span>, <span class="type">int</span>) = sum;  <span class="comment">/* 定义一个函数指针 */</span></span><br><span class="line">c = (*p)(a, b);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;a=%d,b=%d,c=%d\n&quot;</span>, a, b, c);</span><br></pre></td></tr></table></figure>

<h2 id="4-函数指针的存储空间"><a href="#4-函数指针的存储空间" class="headerlink" title="4.函数指针的存储空间"></a><font size=3>4.函数指针的存储空间</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span>; <span class="comment">/* 函数声明 */</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">int</span> a = <span class="number">3</span>;</span><br><span class="line">    <span class="type">int</span> b = <span class="number">2</span>;</span><br><span class="line">    <span class="type">int</span> c = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span> (*p)(<span class="type">int</span>, <span class="type">int</span>) = sum;  <span class="comment">/* 定义一个函数指针 */</span></span><br><span class="line">    c = (*p)(a, b);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;a=%d,b=%d,c=%d\n&quot;</span>, a, b, c);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;a=%p,&amp;b=%p,&amp;c=%p\n&quot;</span>, &amp;a, &amp;b,&amp;c);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;&amp;sum=%p,p=%p,&amp;p=%p\n&quot;</span>, sum, p, &amp;p);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">sum</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> (a + b);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220207104831017.png" alt="image-20220207104831017" style="zoom:67%;" />

<p>由上图分析可以得到函数和函数指针在内存中的分布如下图：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/01HQ%E8%AF%BE%E7%A8%8B%E4%BD%93%E7%B3%BB/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/LV01-06-C%E8%AF%AD%E8%A8%80-%E6%8C%87%E9%92%88/img/image-20220207105826214.png" alt="image-20220207105826214" style="zoom:50%;" />

<h2 id="5-函数指针类型"><a href="#5-函数指针类型" class="headerlink" title="5.函数指针类型"></a><font size=3>5.函数指针类型</font></h2><h3 id="5-1定义格式"><a href="#5-1定义格式" class="headerlink" title="5.1定义格式"></a><font size=3>5.1定义格式</font></h3><p>上边我们已经知道了可以按照下边的形式定义一个函数指针：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;数据类型&gt; (*&lt;函数指针名称&gt;) (&lt;参数说明列表&gt;);</span><br></pre></td></tr></table></figure>

<p>可是当参数列表中参数很多的时候，就显得很麻烦，这个时候我们可以借助 typedef 来定义一个函数指针类型：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typedef</span> &lt;数据类型&gt; (*&lt;函数指针名称&gt;) (&lt;参数说明列表&gt;);</span><br></pre></td></tr></table></figure>

<h3 id="5-2使用实例"><a href="#5-2使用实例" class="headerlink" title="5.2使用实例"></a><font size=3>5.2使用实例</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="title function_">int</span> <span class="params">(*MyFunc)</span><span class="params">(<span class="type">int</span>, <span class="type">char</span>)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func1</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">func2</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> (*p)(<span class="type">int</span>, <span class="type">char</span>);<span class="comment">/* 定义一个可以指向带有int和char类型参数且返回值为int类型的函数指针变量 */</span></span><br><span class="line">	MyFunc pfunc;       <span class="comment">/* 与上边的含义相同 */</span></span><br><span class="line"></span><br><span class="line">	p = func1;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*p)(<span class="number">5</span>, <span class="string">&#x27;a&#x27;</span>));</span><br><span class="line">	p = func2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n\n&quot;</span>, (*p)(<span class="number">6</span>, <span class="string">&#x27;b&#x27;</span>));</span><br><span class="line"></span><br><span class="line">	pfunc = func1;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*pfunc)(<span class="number">5</span>, <span class="string">&#x27;a&#x27;</span>));</span><br><span class="line">	pfunc = func2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*pfunc)(<span class="number">6</span>, <span class="string">&#x27;b&#x27;</span>));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func1</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1:a = %d, b = %c\n&quot;</span>, a, b);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func2</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func2:a = %d, b = %c\n&quot;</span>, a, b);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out </span><br><span class="line">./a.out # 执行可执行程序</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">func1:a = 5, b = a</span><br><span class="line">func1 return:1</span><br><span class="line">func2:a = 6, b = b</span><br><span class="line">func1 return:2</span><br><span class="line"></span><br><span class="line">func1:a = 5, b = a</span><br><span class="line">func1 return:1</span><br><span class="line">func2:a = 6, b = b</span><br><span class="line">func1 return:2</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>可见两种定义其实是完全一致的。</p>
              </div>
            </details>

<h2 id="6-函数指针数组"><a href="#6-函数指针数组" class="headerlink" title="6.函数指针数组"></a><font size=3>6.函数指针数组</font></h2><h3 id="6-1定义"><a href="#6-1定义" class="headerlink" title="6.1定义"></a><font size=3>6.1定义</font></h3><p>函数指针数组是一个保存若干个函数名的数组，一般形式如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;数据类型&gt; (*&lt;函数指针数组名称&gt; [&lt;大小&gt;] )(&lt;参数说明列表&gt; );</span><br></pre></td></tr></table></figure>

<p>其中，&lt;大小&gt;是指函数指针数组元数的个数，其它同普通的函数指针。</p>
<p>例如：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> (*pFunction[<span class="number">3</span>])(<span class="type">int</span>, <span class="type">int</span>);</span><br></pre></td></tr></table></figure>

<p>以上语句定义了一个指针数组  pFunction ，该指针数组的元素都是 int 型函数指针，可以指向返回值类型为  int  型，且有两个整型参数的函数。</p>
<h3 id="6-2使用"><a href="#6-2使用" class="headerlink" title="6.2使用"></a><font size=3>6.2使用</font></h3><details class="folding-tag" blue><summary> 点击查看实例 </summary>
              <div class='content'>
              <figure class="highlight c"><figcaption><span>test.c</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="title function_">int</span> <span class="params">(*MyFunc)</span><span class="params">(<span class="type">int</span>, <span class="type">char</span>)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func1</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span>;</span><br><span class="line"><span class="type">int</span> <span class="title function_">func2</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span>;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> (*p[<span class="number">2</span>])(<span class="type">int</span>, <span class="type">char</span>); <span class="comment">/* 定义一个可以指向带有int和char类型参数且返回值为int类型的函数指针数组 */</span></span><br><span class="line">	MyFunc pfunc[<span class="number">2</span>];        <span class="comment">/* 定义一个可以指向带有int和char类型参数且返回值为int类型的函数指针数组 */</span></span><br><span class="line"></span><br><span class="line">	p[<span class="number">0</span>] = func1;</span><br><span class="line">	p[<span class="number">1</span>] = func2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*p[<span class="number">0</span>])(<span class="number">5</span>, <span class="string">&#x27;a&#x27;</span>));</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n\n&quot;</span>, (*p[<span class="number">1</span>])(<span class="number">6</span>, <span class="string">&#x27;b&#x27;</span>));</span><br><span class="line"></span><br><span class="line">	pfunc[<span class="number">0</span>] = func1;</span><br><span class="line">	pfunc[<span class="number">1</span>] = func2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*pfunc[<span class="number">0</span>])(<span class="number">7</span>, <span class="string">&#x27;c&#x27;</span>));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1 return:%d\n&quot;</span>, (*pfunc[<span class="number">1</span>])(<span class="number">8</span>, <span class="string">&#x27;d&#x27;</span>));</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func1</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func1:a = %d, b = %c\n&quot;</span>, a, b);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">func2</span><span class="params">(<span class="type">int</span> a, <span class="type">char</span> b)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;func2:a = %d, b = %c\n&quot;</span>, a, b);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure><p>在终端执行以下命令编译程序：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">gcc test.c -Wall # 生成可执行文件 a.out </span><br><span class="line">./a.out # 执行可执行程序</span><br></pre></td></tr></table></figure><p>然后，终端会有以下信息显示：</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">func1:a = 5, b = a</span><br><span class="line">func1 return:1</span><br><span class="line">func2:a = 6, b = b</span><br><span class="line">func1 return:2</span><br><span class="line"></span><br><span class="line">func1:a = 7, b = c</span><br><span class="line">func1 return:1</span><br><span class="line">func2:a = 8, b = d</span><br><span class="line">func1 return:2</span><br></pre></td></tr></table></figure>
              </div>
            </details>



<h1 id="六、指针定义总结"><a href="#六、指针定义总结" class="headerlink" title="六、指针定义总结"></a><font size=3>六、指针定义总结</font></h1><h2 id="1-int-p"><a href="#1-int-p" class="headerlink" title="1. int p; "></a><font size=3>1. int p; </font></h2><p>这是一个普通的整型变量p，这个语句的含义就是定义了一个整型变量 p 。</p>
<h2 id="2-int-p"><a href="#2-int-p" class="headerlink" title="2. int *p; "></a><font size=3>2. int *p; </font></h2><ul>
<li>首先从  p  处开始，先与  *  结合，所以说明  p  是一个指针；</li>
<li>然后再与 int  结合, 说明指针所指向的内容的类型为  int  型；</li>
</ul>
<p>所以  p  是一个可以<strong>指向整型数据的指针变量</strong>，这个语句的含义就是定义了一个指向整型数据的指针变量 p。</p>
<h2 id="3-int-p"><a href="#3-int-p" class="headerlink" title="3. int **p; "></a><font size=3>3. int **p; </font></h2><ul>
<li>首先从  p  开始, 先与  *  结合, 说是  p  是一个指针；</li>
<li>然后再与  *  结合, 说明指针所指向的元素是指针；</li>
<li>然后再与  int  结合, 说明该指针所指向的元素是整型数据。</li>
</ul>
<p>所以， p 是一个<strong>二级指针，指向整型数据</strong>，p中可以存放一个地址，这个地址对应的内存区域是一个整数，这个语句的含义就是定义了一个可以指向整型数据的二级指针变量 p 。</p>
<h2 id="4-int-p-3"><a href="#4-int-p-3" class="headerlink" title="4. int p[3]; "></a><font size=3>4. int p[3]; </font></h2><ul>
<li>首先从  p  处开始，先与  []  结合，说明  p  是一个数组；</li>
<li>然后与  int  结合, 说明数组里的元素是整型的。</li>
</ul>
<p> 所以  p  是一个由整型数据组成的<strong>数组</strong>，这个语句的含义就是定义了一个含有 3 个整型数据的数组 p 。</p>
<h2 id="5-int-p-3"><a href="#5-int-p-3" class="headerlink" title="5. int *p[3]; "></a><font size=3>5. int *p[3]; </font></h2><ul>
<li>首先从  p  处开始，先与  []  结合，因为其优先级比  *  高，所以  p  是一个数组；</li>
<li>然后再与  *  结合，说明数组里的元素是指针类型；</li>
<li>然后再与 int 结合，说明指针所指向的内容的类型是整型的。</li>
</ul>
<p>所以  p  是一个由<strong>指向整型数据的指针所组成的数组</strong>，这个语句的含义就是，定义了一个含有 3 个元素的数组 p ，数组中每个元素都是一个指向整型数据的指针。</p>
<h2 id="6-int-p-3"><a href="#6-int-p-3" class="headerlink" title="6. int (*p)[3]; "></a><font size=3>6. int (*p)[3]; </font></h2><ul>
<li>首先从  p  处开始，先与  *  结合，说明  p  是一个指针；</li>
<li>然后再与  []  结合（与”()”这步可以忽略，只是为了改变优先级）， 说明指针所指向的内容是一个数组；</li>
<li>然后再与 int  结合, 说明数组里的元素是整型的。</li>
</ul>
<p>所以  p  是一个<strong>指向由整型数据组成的数组的指针</strong>，这个语句的含义就是，定义了一个指针变量 p ，它可以指向一个含有 3 个 int 类型元素的数组。</p>
<h2 id="7-int-p-int"><a href="#7-int-p-int" class="headerlink" title="7. int p(int); "></a><font size=3>7. int p(int); </font></h2><ul>
<li>首先，从  p  处起,先与  ()  结合, 说明  p  是一个函数；</li>
<li>然后进入  ()  里分析，说明该函数有一个整型变量的参数；</li>
<li>然后再与外面的  int  结合, 说明<strong>函数的返回值是一个整型数据</strong>。</li>
</ul>
<p>所以， p 是一个带有一个 int 形参且返回值为 int 类型的函数，这个语句的含义就是，定义了一个函数 p，这个函数有一个 int 类型的参数，返回值为 int 类型。</p>
<h2 id="8-int-p-int"><a href="#8-int-p-int" class="headerlink" title="8. int *p(int); "></a><font size=3>8. int *p(int); </font></h2><ul>
<li>首先，从  p  处起,先与  ()  结合，说明  p  是一个函数；</li>
<li>然后进入  ()  里分析，说明该函数有一个整型变量的参数；</li>
<li>然后再与外面的  int  *结合, 说明<strong>函数的返回值是一个整型指针</strong>。</li>
</ul>
<p>所以， p 是一个带有一个 int 形参且返回值为 int *类型的函数，这个语句的含义就是，定义了一个指针函数 p，这个函数有一个 int 类型的参数，返回值为 int *类型。</p>
<h2 id="9-int-p-int"><a href="#9-int-p-int" class="headerlink" title="9. int (*p)(int); "></a><font size=3>9. int (*p)(int); </font></h2><ul>
<li>首先，从  p  处开始, 先与指针结合，说明  p  是一个指针；</li>
<li>然后与 () 结合，说明指针指向的是一个函数；</li>
<li>然后再与 () 里的  int  结合, 说明函数有一个 int  型的参数；</li>
<li>再与最外层的 int 结合, 说明函数的返回类型是整型。</li>
</ul>
<p>所以  p  是一个<strong>指向有一个整型参数且返回类型为整型的函数的指针</strong>，这个语句的含义就是，定义了一个函数指针 p，这个函数指针指向一个函数，这个函数有一个 int 类型参数，且返回值为 int 类型。</p>
<h2 id="10-int-p-3-int-int"><a href="#10-int-p-3-int-int" class="headerlink" title="10. int (*p[3])(int, int); "></a><font size=3>10. int (*p[3])(int, int); </font></h2><ul>
<li>首先从  p  处开始, 先与  []  结合，因为其优先级比  *  高，所以  p  是一个数组；</li>
<li>然后再与  *  结合, 说明数组里的元素是指针类型；</li>
<li>然后与 () 结合, 说明指针指向的是一个函数；</li>
<li>然后再与 () 里的  int  结合, 说明函数有两个 int  型的参数；</li>
<li>再与最外层的  int  结合, 说明函数的返回类型是整型。</li>
</ul>
<p>所以  p  是一个指向有两个整型参数且返回类型为整型的<strong>函数的指针数组</strong>，这个语句的含义就是，定义了一个有 3 个元素的数组 p ，数组中每个元素都是指针，指针指向含有两个 int 类型参数，且返回值为 int 类型的函数。</p>
<h2 id="11-int-p-int-3"><a href="#11-int-p-int-3" class="headerlink" title="11. int *(*p(int))[3]; "></a><font size=3>11. int *(*p(int))[3]; </font></h2><ul>
<li>首先，从  p  开始,先与  ()  结合, 说明  p  是一个函数；</li>
<li>然后进入  ()  里面，与  int  结合，说明函数有一个整型变量参数；</li>
<li>然后再与外面的  *  结合, 说明函数返回的是一个指针；</li>
<li>然后到最外面一层, 先与 [] 结合, 说明返回的指针指向的是一个数组,；</li>
<li>然后再与  *  结合, 说明数组里的元素是指针；</li>
<li>然后再与  int  结合, 说明指针指向的内容是整型数据。</li>
</ul>
<p>所以  p  是一个<strong>参数为一个整型数据且返回一个指向由整型指针变量组成的数组的指针变量的函数</strong>。</p>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/84847102.html">LV01-06-C语言-指针</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2022年01月15日 - 16:29</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/84847102.html" title="LV01-06-C语言-指针">https://sumumm.github.io/post/84847102.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV01-C%E8%AF%AD%E8%A8%80%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> LV01-C语言基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/203e48a8.html" rel="prev" title="LV01-07-C语言-函数">
                  <i class="fa fa-angle-left"></i> LV01-07-C语言-函数
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/582937ae.html" rel="next" title="LV01-05-C语言-数组与字符串">
                  LV01-05-C语言-数组与字符串 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
